#include "mainscene.h"
#include "ui_mainscene.h"
#include <QSound>
#include <QPainter>
#include <QTimer>
#include "db.h"
#include <QPushButton>
#include <QDebug>
#include "flatui.h"
#include "ifmform.h"
#include <QMessageBox>
#include "loginform.h"

MainScene::MainScene(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);
    this->f = new IfmForm();
    this->lf = new LoginForm();
    this->initForm();
}
MainScene::~MainScene()
{
    delete ui;
}

void MainScene ::setBackgroundImage(const QString &path)
{
    QPixmap pixmap = QPixmap(path).scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background, QBrush(pixmap));
    this->setPalette(palette);
}

void MainScene::initForm()
{   //设置全屏模式
    this->showFullScreen();
    //设置背景
    //this->setBackgroundImage(":/image/background.png");
    //设置布局
    ui->label_Search->move(this->width()* 3 / 4.0,this->height()*1 / 6.0 + 60);
    ui->lineEdit_Search->move(this->width()* 3 / 4.0,this->height()*1 / 6.0);
    FlatUI::setLineEditQss(ui->lineEdit_Search, 5, 2, "#DCE4EC", "#1ABC9C");
    ui->pushButton_Search->move(this->width()* 3 / 4.0,this->height()*1 / 4.0);
    ui->pushButton_Admin->move(this->width()* 3 / 4.0,this->height()*2 / 4.0);
    ui->pushButton_Reflash->move(this->width()* 3 / 4.0,this->height()*3 / 4.0);
    ui->tableWidget->move(0,0);
    ui->tableWidget->resize(this->width()* 3 / 4.0, this->height());
    ui->tableWidget->setFrameShape(QFrame::NoFrame); //无边框
    ui->tableWidget->setShowGrid(false); //去掉格子线
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);  //禁止修改
    ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);  //禁止选中
    this->initIfm();
}

void MainScene::initIfm()
{
    ui->tableWidget->clear();
    QStringList headerText = Db::getCategoryName();
    ui->tableWidget->setColumnCount(headerText.count());
    ui->tableWidget->setHorizontalHeaderLabels(headerText);
    int maxDepth = 1;
    QHash<int, int> columnDepth;
    QSqlQuery dataSet = Db::getProductNameAndCno();
    while(dataSet.next())
    {
        ui->tableWidget->setRowCount(maxDepth);
        int cno = dataSet.value("cno").toInt();
        QPushButton *newbtn = new QPushButton(dataSet.value("pname").toString());
        connect(newbtn,&QPushButton::clicked,[=](){
            //按钮音效
            QSound *closeSound = new QSound(":/sound/TapButtonSound.wav",this);
            closeSound->play();
            QTimer::singleShot(300, this,[=]()
            {
                if(!this->f->isHidden()) f->close();
                if(!this->lf->isHidden()) lf->close();
                QString t = newbtn->text();
                this->f->initForm(t);
                this->f->show();
            });
        });
        FlatUI::setPushButtonQss(newbtn); //美化
        ui->tableWidget->setCellWidget(columnDepth[cno],cno - 1,newbtn);
        columnDepth[cno] =  columnDepth[cno] + 1;
        if (columnDepth[cno] > maxDepth - 1) maxDepth++;
    }
    ui->tableWidget->setRowCount(maxDepth);
}

void MainScene::on_pushButton_Reflash_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/BackButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(500, this,[=](){
        this->initIfm();
    });
}

void MainScene::on_pushButton_Search_clicked()
{
    QSound *closeSound = new QSound(":/sound/BackButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
        QSqlQuery dataSet = Db::getProductIfm(ui->lineEdit_Search->text());
        if(!dataSet.next())
        {
            if(!this->f->isHidden()) f->close();
            if(!this->lf->isHidden()) lf->close();
            ui->label_Search->setText("no find");
        }
        else
        {
            if(this->f)
            {
                ui->label_Search->setText("find");
                if(!this->f->isHidden()) f->close();
                if(!this->lf->isHidden()) lf->close();
                this->f->initForm(ui->lineEdit_Search->text());
                this->f->show();
            }
        }
    }
   );
}

void MainScene::on_pushButton_Admin_clicked()
{
    QSound *closeSound = new QSound(":/sound/BackButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
        if(!this->f->isHidden()) this->f->close();
        if(this->lf->isHidden())
        {
            this->lf->closeAdminForm();
            this->lf->clearLineEdit();
            this->lf->reflashPIN();
            this->lf->show();
        }
        else
        {
            this->lf->closeAdminForm();
            this->lf->close();
        }
    }
    );
}
